<?php

namespace App\Kuafu\WaiMai\Model;

use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Base\Model\Traits\KfWaiMaiSplit;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Kuafu\Base\Model\BaseModel;
use Illuminate\Database\Eloquent\Relations\HasMany;

class WaiMaiShopTimeConfigModel extends BaseModel
{
    use HasFactory;
    use KfUid;

    protected $connection = 'mysqlWaiMaiAll';
    protected $table      = 'waimai_shop_time_config'; //表名
    protected $primaryKey = 'id';
    public    $timestamps = true;

    protected $fillable = [
        'shopId',
        'timeType',
        'timeStart',
        'timeEnd',
    ];

    const TIME_TYPE_1 = 1;
    const TIME_TYPE_2 = 2;
    const TIME_TYPE_3 = 3;

    const TIME_TYPE_TEXT = [
        self::TIME_TYPE_1 => '闲时',
        self::TIME_TYPE_2 => '午市',
        self::TIME_TYPE_3 => '晚市',
    ];

    public function finishConfigRel(): HasMany
    {
        return $this->hasMany(WaiMaiShopFinishConfigModel::class, 'timeId', 'id');
    }

    public function getTimeTypeTextAttribute(): string
    {
        return self::TIME_TYPE_TEXT[$this->timeType] ?? '';
    }

    public static function addData(int $shopId, int $timeType, string $timeStart, string $timeEnd)
    {
        return self::query()
                   ->create([
                       'shopId'    => $shopId,
                       'timeType'  => $timeType,
                       'timeStart' => $timeStart,
                       'timeEnd'   => $timeEnd
                   ]);
    }

    public static function delData(int $shopId)
    {
        return self::query()
                   ->where('shopId', $shopId)
                   ->delete();
    }
}
